﻿using Autodesk.AutoCAD.DatabaseServices;
using HaierCADplug_in.Model;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace HaierCADplug_in.View
{
    /// <summary>
    /// SettingsWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SettingsWindow : Window
    {
        private XElement _settings;
        private ConfigManager _configManager;
        public SettingsWindow()
        {
            InitializeComponent();
            if (!GlobalVariables.UserIsActive)
            {
                MessageBox.Show("请先登录");
            }
            else
            {
                _configManager = new ConfigManager("Settings.xml");
                LoadSettings();
            }

        }
        private void LoadSettings()
        {
            try
            {
                _settings = _configManager.LoadSettings();
                //usernameTextBox.Text = _settings.Element("Username")?.Value;
                //emailTextBox.Text = _settings.Element("Email")?.Value;
                //themeComboBox.SelectedItem = _settings.Element("Theme")?.Value;
                //fontSizeSlider.Value = Double.Parse(_settings.Element("FontSize")?.Value ?? "14");
                //loggingCheckBox.IsChecked = Boolean.Parse(_settings.Element("LoggingEnabled")?.Value ?? "false");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            // 根据sender判断是哪个标签的保存按钮
            var parentTab = FindParent<TabItem>((Button)sender);
            if (parentTab != null)
            {
                switch (parentTab.Header)
                {
                    case "Personal":
                        SavePersonalSettings();
                        break;
                    case "Preferences":
                        SavePreferencesSettings();
                        break;
                    case "Advanced":
                        SaveAdvancedSettings();
                        break;
                }
            }
            //_settings.SetElementValue("Username", usernameTextBox.Text);
            //_settings.SetElementValue("Email", emailTextBox.Text);
            //_settings.SetElementValue("Theme", themeComboBox.SelectedItem);
            //_settings.SetElementValue("FontSize", fontSizeSlider.Value.ToString());
            //_settings.SetElementValue("LoggingEnabled", loggingCheckBox.IsChecked.ToString());
            _configManager.SaveSettings(_settings);
        }

        private void SavePersonalSettings()
        {
            // 保存个人设置的逻辑
        }

        private void SavePreferencesSettings()
        {
            // 保存偏好设置的逻辑
        }

        private void SaveAdvancedSettings()
        {
            // 保存高级设置的逻辑
        }

        private static T FindParent<T>(DependencyObject child) where T : DependencyObject
        {
            DependencyObject parent = VisualTreeHelper.GetParent(child);
            if (parent == null) return null;
            if (parent is T parentTyped) return parentTyped;
            return FindParent<T>(parent);
        }

    }
}
